package message;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

import client.BaseClient;
import client.TankClient;

/**
 * 消息处理类
 * @ClassName:       MessageHandler
 * @Description:     TODO
 * @author:          ljh
 * @version:         CMR--
 * @date:            2019年2月21日        下午6:37:25
 */
public class MessageHandler {

	private static final MessageHandler handler = new MessageHandler();
	private final static BlockingQueue<MessageInfo> messageQueue = new LinkedBlockingQueue<>(1000);
	//消息客户端
	private static final List<String> clients = new ArrayList<>();
	
	private MessageHandler() {}
	
	/**
	 * 单例模式处理
	 * @author:       lijiahao
	 * @date:         [2019年2月21日] 下午6:38:41
	 * @Description:  [TODO]
	 * @param:        @return
	 */
	public static MessageHandler getInstance() {
		return handler;
	}
	
	public void registryClient(String clientId) {
		System.out.println("客户端正在注册消息中心");
		clients.add(clientId);
		System.out.println(clientId+" 注册成功！");
	}
	
	public void transfor(MessageInfo info) {
		messageQueue.offer(info);
	}
	
	public MessageInfo getMessageInfo() {
		MessageInfo message = null;
		try {
			message = messageQueue.take();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		return message;
	}
}
